package com.heima.article.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.service.ApAuthService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/apauth")
public class ApAuthController {

    @Autowired
    private ApAuthService apAuthService ;


    /**
     * 根据id查询作者
     * @param id
     * @return
     */
    @GetMapping("/find/{id}")
    public ApAuthor findById(@PathVariable ("id") Integer id){

        LambdaQueryWrapper<ApAuthor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApAuthor::getId,id);

        List<ApAuthor> list = apAuthService.list(lambdaQueryWrapper);

        if(list != null && !list.isEmpty() ){
            return list.get(0);
        }

        return null;
    }


    /**
     *
     * 增加文章作者
     * @param apAuthor
     * @return
     */
    @PostMapping("/save")
    public ResponseResult saveApAuther(@RequestBody ApAuthor apAuthor){
        boolean save = apAuthService.save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
